<template>
  <div class="container">
    <div class="title">
      <h2>楼栋管理</h2>
      <h5>用于展示小区里每一栋的基础信息</h5>
    </div>
    <!-- 标题 -->
    <el-card class="box-card">
      <el-form ref="form" :model="formData" class="demo-form-inline">
        <el-form-item label="小区名称">
          <el-select v-model="formData.community_code" placeholder="请输入">
            <el-option
              v-for="item in selectedAll"
              :key="item.id"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="栋数名称">
          <el-input v-model="formData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="创建时间">
          <div class="block">
            <el-date-picker
              v-model="value"
              value-format="timestamp"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="changeTime"
            />
          </div>
        </el-form-item>
        <el-form-item class="btn">
          <el-button
            style="float: right; padding: 10px 20px"
            type="primary"
            @click="onSubmit"
          >查询</el-button>
          <el-button
            style="float: right; padding: 10px 20px; margin-right: 10px"
            @click="onReset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="tableCard">
      <el-button
        style="float: right; padding: 10px 20px; margin-right: 80px"
        type="primary"
        size="small"
        :disabled="!checkBtnPermission('/house/building','create')"
        @click="$refs.addForm.diailogShow = true"
      >新增项目</el-button>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="code" label="栋数编号" width="350" />
        <el-table-column prop="community.name" label="小区名称" width="180" />
        <el-table-column prop="name" label="栋数名称" />
        <el-table-column prop="lift" label="电梯数" />
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | conversion }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="{row}">
            <el-button
              type="text"
              size="small"
              :disabled="!checkBtnPermission('/house/building','update')"
              @click="handleClick(row.id)"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              :disabled="!checkBtnPermission('/house/building','destroy')"
              @click="delRow(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <br>
      <!-- 页面渲染 -->
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          :page-sizes="[5,10, 15, 20, 30]"
          :page-size="formData.pageSize"
          :current-page="formData.current"
          layout="total, sizes, prev, pager, next, jumper"
          :total="count"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-row>
    </el-card>
    <!-- 新增、修改弹框区域 -->
    <addForm ref="addForm" @updataList="initData" />
  </div>
</template>

<script>
import { communityInfo, houseInfo, delHouseInfo, getHouseInfoByid } from '@/api/house/house_building'
import addForm from './components/building.vue'
export default {
  name: 'Building',
  components: {
    addForm
  },
  // 表单时间过滤器
  filters: {
    // 3.定义一个处理函数，参数value为要处理数据
    conversion: function(value) {
      // 调用Date的方法，处理时间戳
      return new Date(value).toLocaleString()
    }
  },

  data() {
    return {
      formData: {
        current: 1,
        pageSize: 10,
        create_time: '', // 创建时间
        update_time: '',
        name: '', // 栋数名称
        code: '', // 栋数编号
        lift: '', // 电梯数
        community_code: '',
        // id: ''// 栋数编号
        community: {
          name: ''// 小区名称
        }

      }, // 表单
      count: 0,
      list: [], // 获取列表
      selectedAll: [], // 下拉框获取
      value: '' // 获取时间
      // diailogShow: false // 弹框
    }
  },
  created() {
    this.communityInfo()
    this.initData()
  },

  methods: {
    // 搜索重置
    onReset() {
      // 重置搜索表单
      this.formData = {
        current: 1,
        pageSize: 10,
        create_time: '', // 创建时间
        update_time: '',
        community: '', // 小区名称
        name: '', // 车位名称
        code: '', // 栋数编号
        lift: '' // 电梯数
      }
      this.value = ''
      // 拉取列表
      this.initData()
    },
    // 搜索表单
    onSubmit() {
      this.initData()
    },
    // 下拉框
    async communityInfo() {
      const { data } = await communityInfo()
      // console.log('社区', data.data)
      this.selectedAll = data.data
    },
    // 创建时间获取 时间数据处理
    changeTime(value) {
      console.log('1111', value)
      const temp = []
      temp.push(value[0])
      temp.push(value[1])
      this.formData.create_time = temp.join(',')
    },

    // 数据渲染
    async initData() {
      const { data } = await houseInfo(this.formData)
      // console.log('initData  data:', data.data)
      this.list = data.data.rows
      this.count = data.data.count
      // this.$refs.addForm.list = res.rows // 先把值传给子组件
      // console.log('对比', this.$refs.addForm.list)
    },
    // 编辑
    async handleClick(rowId) {
      // 获取改行的数据
      // console.log(11)
      const { data } = await getHouseInfoByid(rowId)
      // console.log(data)
      // 赋给弹框里面的表单
      // console.log(this.$refs.addForm.quartData)
      this.$refs.addForm.quartData = data.data
      // console.log('editRow  res:', data.data)
      // 复杂点：一些负责的表单控件需要手动去回显
      // 打开弹框
      this.$refs.addForm.diailogShow = true
    },
    // 删除
    async delRow(rowId) {
      console.log('1111', rowId)
      // 弹出确认框
      await this.$confirm('确认删除吗')
      // 发生删除请求
      const { data } = await delHouseInfo(rowId)
      // 提示成功
      // console.log(data.success)
      if (!data.success) {
        return this.$message.error(data.msg)
      }
      this.$message.success('删除成功')
      // 列表更新
      this.initData()
    },
    // 点击切换页数
    handleCurrentChange(val) {
      this.formData.current = val
      this.initData()
    },

    // 点击切换数量
    handleSizeChange(val) {
      this.formData.pageSize = val
      this.initData()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 40px;
  .title {
    display: flex;
    align-items: center;
    h2 {
      margin-right: 20px;
    }
    h5 {
      font-weight: 400;
      color: #9c9c9d;
      vertical-align: middle;
    }
  }
  .demo-form-inline {
    display: flex;
  }
  .el-form-item {
    margin-right: 73px;
  }
  .btn {
    padding-top: 40px;
  }
  .box-card {
    margin-bottom: 10px;
  }
}

.block{
  margin-top: 30px;
  text-align: right;
}
</style>
